package com.fattyCorps.battle.service.battle.behavior.condition;

import cn.hutool.core.util.RandomUtil;
import com.fattyCorps.battle.service.battle.behavior.Behavior;
import com.fattyCorps.battle.service.battle.behavior.BhvProcessor;
import com.fattyCorps.battle.service.battle.data.BattleHero;
import com.fattyCorps.common.enums.battle.EBattleAttType;
import com.fattyCorps.common.enums.battle.EBehaviorType;
import com.fattyCorps.common.enums.battle.triggerCondition.EBehaviorCondition;
import com.fattyCorps.common.obj.config.BehaviorTrgCndMould;
import com.fattyCorps.common.obj.config.TrgCndMould;
import com.fattyCorps.common.util.GameCfgUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class BhvTrgCnd extends TrgCnd {
    @Override
    protected TrgCndMould getMould(Integer id) {
        return cfg.getCfgOrNull(BehaviorTrgCndMould.class, id);
    }

    public boolean canTrigger(BhvProcessor.Event e, BattleHero bhvOwner, Behavior bhv) {
        EBehaviorCondition cnd = e.getCnd();
        BehaviorTrgCndMould cndMould = (BehaviorTrgCndMould)this.mould;

        // 基础条件类型
        if (!cndMould.getCombination()) {
            if (!cndMould.getCnd().equals(cnd)) {
                return false;
            }

            if (RandomUtil.randomInt(1, 100) > cndMould.getPer()) {
                return false;
            }

            // 对于自己有效的事件，需要判断施法者与行为拥有者是否相同，如：触发事件是-当自己普通攻击时，事件的触发者与行为拥有者都要是自己才行
            if (cndMould.getCnd().isSelf() && !bhvOwner.equals(e.getTrigger())) {
                return false;
            }

            // 无法普通攻击时，反击类伤害也无法触发
            if (!bhvOwner.canActive(EBattleAttType.NorAtt) && bhv.getMould().getType().equals(EBehaviorType.CounterAttack)) {
                return false;
            }

            if (cnd.equals(EBehaviorCondition.AfterSelfHpBelow)) {
                BigDecimal per = new BigDecimal(cndMould.getParam());
                BigDecimal maxHp = new BigDecimal(bhvOwner.getHero().getPropComp().getMaxHp());
                int trgHp = Integer.valueOf(per.multiply(maxHp).divide(BigDecimal.valueOf(100L)).toString());
                return bhvOwner.getHp() < trgHp;
            }
//            else if (cnd.equals(EBehaviorCondition.AfterRoundEnd)) {
//                return true;
//            }

            return true;
        } else {
            List<Integer> childrenIds = GameCfgUtils.splitIdListStr(mould.getChildren());
            List<Boolean> childRets = new ArrayList<>();
            for (Integer childId : childrenIds) {
                BhvTrgCnd childCond = new BhvTrgCnd();
                childCond.init(childId, this.battle);
                childRets.add(childCond.canTrigger(e, bhvOwner, bhv));
            }

            switch (cndMould.getChildrenAndOr()) {
                case And:
                    return !childRets.contains(Boolean.FALSE);
                case Or:
                    return childRets.contains(Boolean.TRUE);
            }
        }

        return true;
    }
}

